More often than we realize, we see these memes teasing how difficult it is to quit vim if you are new to vim. What better way to start writing about vim than to show “How to Quit Vim”.
Quitting is nothing but closing the open (and hidden) files in vim. There are multiple ways to quit vim:
1. :q
If only 1 file is open and there are no changes to the file, you can quit vim using the below command:
<ESC>:q
If there are any unsaved changes, vim will prompt you saying it cannot exit. Suppose if there are multiple files open in different tabs then it will close the file in the current tab.
2. :q!
If the above command failed to quit vim, you force vim to quit without saving using the below command:
<ESC>:q!
The exclamatory symbol force quits vim without saving. As before, if multiple files are open then it will close the file in the current tab.
3. :qa[ll]
What if there are multiple files that are open and we need to close all the files and quit vim? One way is you keep on hitting :q until all the open files are closed or, alternatively use the below command to quit all the files in one go:
<ESC>:qall
You can also use the shorter version:
<ESC>:qa
But be aware, similar to :q this will close all the files only if there are no unsaved files. If there are then vim will prompt you that it cannot quit.
4. :qa[ll]!
I guess by now this is pretty self explanatory, it will close all the files before quitting vim even if there are unsaved changes. The unsaved changes will be lost.
5. :wq <or> : x
If you wish to save and close the file then use the below command:
<ESC>:wq
You can also use the single letter command:
<ESC>:x
This will save the open file in the current tab and close that file. This will quit vim only if there are only 1 tab that are open. If there are multiple files open in different tabs, then vim will close the current file and bring the file in the next tab ready to be edited.
6. :wqa[ll]
This is pretty self explanatory , it will save all the files and close all the files before quitting vim. Unlike :wq which might or might not quit vim, this will quit vim after taking care of all the files.
Hope after reading this article you will never be stuck inside vim again.